<?php
namespace app\admin\model;

use think\Model;

class CryptoNews extends Model
{
    protected $attributes = [
        'platform' => [
            1 => 'facebook',
            2 => 'twitter',
            3 => 'ins',
            4 => 'vk',
            5 => 'tiktok'
        ],
        'language' => [
            0 => 'English',
            1 => 'Russian',
            2 => 'Spanish',
            3 => 'Chinese',
            4 => 'French'
        ],
        'status' => [
            0 => '未使用',
            1 => '已使用'
        ],
        'task_status' => [
            0 => '未执行',
            1 => '已执行'
        ],
        'publish_status' => [
            1 => '未发布',
            2 => '发布中',
            3 => '发布成功',
            4 => '发布失败'
        ]
    ];

    public function publish()
    {
        return $this->hasMany(MarketSmCopywriting::class, 'crypto_news_id', 'id')->order('id desc')->append(['platform_value']);
    }

    public function marketNewsSearchDet()
    {
        return $this->hasMany(MarketNewsSearchDet::class, 'crypto_news_id', 'id');
    }

    public function getFieldEnum($field, $func = null)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'channel' => $this->getChannelEnum(),
                'status' => $this->attributes['status'],
                'language' => $this->getLanguageEnum(),
                'mining_machine' => extractListFromData($func()),
                'task_status' => transLabel($this->attributes['task_status']),
                'publish_status' => transLabel($this->attributes['publish_status'])
            ];
        } else {
            if ($field == 'mining_machine') {
                return [$field => extractListFromData($func())];
            }
            return [$field => $this->attributes[$field]];
        }
    }

    public function getChannelEnum(){
        $data = [];
        $oSystemSmChannel = SystemSmChannel::where([['status','=',1],['is_del','=',0]])->column('id,name');
        foreach ($oSystemSmChannel as $value){
            $data[] = ['label'=>$value['name'],'value'=>$value['id']];
        }
        return $data;
    }

    public function getLanguageEnum(){
        $data = [];
        $oSystemSmChannel = SystemLanguage::where([['status','=',1],['is_del','=',0]])->column('id,english_name as name');
        foreach ($oSystemSmChannel as $value){
            $data[] = ['label'=>$value['name'],'value'=>$value['id']];
        }
        return $data;
    }

    public function transLabel($data)
    {
        $result = [];
        foreach ($data as $key => $value) {
            $result[] = [
                'label' => $value,
                'value' => $key
            ];
        }
        return $result;
    }
}

?>
